package cn.com.qc.main;


import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.logging.SimpleFormatter;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.qc.R;
import cn.com.qc.app.App;
import cn.com.qc.bean.MyIntroductionBean;
import cn.com.qc.bean.UserIntroInfo;
import cn.com.qc.help.NetUrl;
import cn.com.qc.yinter.MyIntroductionDataListener;
import cn.com.qc.ypresenter.MyIntroductionPresenter;

import static android.util.TypedValue.COMPLEX_UNIT_DIP;

public class MyIntroductionActivity extends YBaseActivity<MyIntroductionPresenter,MyIntroductionActivity> implements MyIntroductionDataListener {

    @BindView(R.id.bar)
    public RelativeLayout bar;
    @BindView(R.id.icon)
    ImageView icon;
    @BindView(R.id.nick)
    TextView nick;

    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.sex)
    TextView sex;
    @BindView(R.id.edu)
    TextView edu;
    @BindView(R.id.nature)
    TextView nature;
    @BindView(R.id.outYear)
    TextView outYear;
    @BindView(R.id.city)
    TextView city;
    @BindView(R.id.connectPhone)
    TextView connectPhone;
    @BindView(R.id.connectEmail)
    TextView connectEmail;

    @BindView(R.id.workLinear)
    LinearLayout workLinear;
    @BindView(R.id.selfIntroLinear)
    LinearLayout selfIntroLinear;
    @BindView(R.id.eduLinear)
    LinearLayout eduLinear;
    String college = "";
    String major = "";
    String start = "";
    String end = "";
    String sno = "";

    String jobName = "";
    String jstart = "";
    String jend = "";
    String jcontent = "";
    @Override
    public MyIntroductionPresenter getPresenter() {
        return new MyIntroductionPresenter();
    }

    @Override
    public int getResId() {
        return R.layout.activity_self_info;
    }

    //18237191735 4477e11c3dd547848f471d92366df099

    App app;

    @Override
    public void init() {
        app = (App) getApplication();
        userIntroInfo = new UserIntroInfo();
        //p.getAllDatas(NetUrl.DNS + NetUrl.GetInfo,app.id,app.token);
        OkGo.<String>post(NetUrl.DNS + NetUrl.GetInfo)
                .tag(this)
                .params("id", app.id)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response.body());
                            Log.d("用户信息界面",jsonObject.toString());
                            int infoCode = jsonObject.getInt("status");
                            if (infoCode == 0) {
                                JSONObject data = jsonObject.optJSONObject("data");
                                JSONObject user = data.optJSONObject("user");
                                name.setText("姓名："+user.optString("realName"));
                                Integer sexs = user.optInt("sex");
                                if (sexs == 0){
                                    sex.setText("性别：女");
                                }else{
                                    sex.setText("性别：男");
                                }
                                outYear.setText("出生日期："+user.optString("birth"));
                                city.setText("所在城市："+user.optString("city"));
                                connectPhone.setText("联系电话："+user.optString("phone"));
                                connectEmail.setText("联系邮箱："+user.optString("email"));
                                nature.setText("民族："+user.optString("nature"));
                                String headImg = user.optString("photo");
                                int width = (int) TypedValue.applyDimension(COMPLEX_UNIT_DIP,58,getResources().getDisplayMetrics());
                                p.intoCircle(MyIntroductionActivity.this,headImg,icon, R.mipmap.head_img,width,width);
                                JSONArray education = data.optJSONArray("education");
                                for (int i = 0;i<education.length();i++){
                                    JSONObject education_item = education.optJSONObject(i);
                                    college = education_item.optString("college");
                                    major = education_item.optString("major");
                                    start = education_item.optString("start");
                                    end = education_item.optString("end");
                                    sno = education_item.optString("sno");
                                    edu.setText("最高学历："+college);
                                }
                                if (education.length() != 0){
                                    View view = LinearLayout.inflate(MyIntroductionActivity.this,R.layout.edu_item,null);
                                    TextView beginTime = (TextView) view.findViewById(R.id.beginTime);
                                    TextView endTiem = (TextView) view.findViewById(R.id.endTiem);
                                    TextView schoolName = (TextView) view.findViewById(R.id.schoolName);
                                    //    TextView education = (TextView) view.findViewById(R.id.education);
                                    TextView majors = (TextView) view.findViewById(R.id.major);

                                    setText(schoolName,"院校名称：",college);
                                    //setText(this.edu,"最高学历：",educa);
                                    setText(majors,"专业：",major);
                                    setText(beginTime,"入学时间：",start);
                                    setText(endTiem,"毕业时间：",end);
                                    eduLinear.addView(view);
                                }

                                JSONArray experience = data.optJSONArray("experience");
                                for (int i = 0;i<experience.length();i++){
                                    JSONObject experience_item = experience.optJSONObject(i);
                                    jobName = experience_item.optString("jobName");
                                    jstart = experience_item.optString("start");
                                    jend = experience_item.optString("end");
                                    jcontent = experience_item.optString("content");
                                }
                                if (experience.length() != 0){
                                    View view = LinearLayout.inflate(MyIntroductionActivity.this,R.layout.work_experience_item,null);
                                    TextView position = (TextView) view.findViewById(R.id.position);
                                    TextView beginDate = (TextView) view.findViewById(R.id.enterDate);
                                    TextView endDate = (TextView) view.findViewById(R.id.endDate);
                                    TextView workContent = (TextView) view.findViewById(R.id.workContent);
                                    setText(position,"职位：",jobName);
                                    setText(beginDate,"入职时间：",jstart);
                                    setText(endDate,"离职时间：",jend);
                                    setText(workContent,"工作内容：",jcontent);
                                    workLinear.addView(view);
                                }

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @OnClick({R.id.returns,R.id.set})
    public void click(View view){
        switch (view.getId()){
            case  R.id.returns:
                finish();
                break;
            case R.id.set:
                Bundle bundle = new Bundle();
                bundle.putString("phone",app.phone);
                bundle.putString("id",app.id);
                bundle.putSerializable("object",userIntroInfo);
                openActivity(EditIntroductionActivity.class,bundle);
                break;
        }
    }

    UserIntroInfo userIntroInfo;

    @Override
    public void onEdu(List<MyIntroductionBean.Education> educations) {

    }

    @BindView(R.id.exceptWorkLinear)
    LinearLayout exceptWorkLinear;

    @Override
    public void onHopeJob(List<MyIntroductionBean.HopeJob> hopeJobs) {

    }

    @Override
    public void onUser(MyIntroductionBean.User student) {

    }

    @Override
    public void onWork(List<MyIntroductionBean.Work> works) {

    }

    @Override
    public void onError() {

    }

    private void setText(TextView textView, String t,String content){
        if(content == null){
            textView.setText(t);
        }else{
            textView.setText(t + content);
        }
    }

}
